// JavaScript Document
(function($){
	var intId = [];
	var bakObj = '';
	$.fn.myScrollStop = function(){
		$.each(intId,function(i,item){
			console.log(item);
			clearInterval(item);
		})
	};

	$.fn.scroll = function(options){
		//默认配置
		var defaults = {
			speed:40,  
			rowHeight:24 
		};
		
		var opts = $.extend({}, defaults, options);

		function marquee(obj, step){
			obj.find("ul").animate({
				marginTop: '-=1'
			},0,
			function(){
				// console.log('animate');
				var s = Math.abs(parseInt($(this).css("margin-top")));
				if(s >= step){
					let li = $(this).find("li:nth-child(1)");
					li.remove();
					$(this).css("margin-top", 0);
				}
			});
		}
		
		//备份
		function bak(obj) {
			bakObj = obj.html();
		}
		
		//恢复
		function restore(obj) {
			obj.html(bakObj);
		}
		
		//滚动定时(到底时持续3秒后重置列表, 再次滚动)
		function setMyInterval(i, obj, rowHeight, speed) {
			intId[i] = setInterval(function(){
				if(obj.find("ul").height()<=obj.height()){
					clearInterval(intId[i]);
					setTimeout(function() {
						restore(obj);
						clearInterval(intId[i]);
						setMyInterval(i, obj, rowHeight, speed);
					},3000);
				}else{
					marquee(obj, rowHeight);
				}
			}, speed);
			
			obj.hover(function(){
				clearInterval(intId[i]);
			},function(){
				clearInterval(intId[i]);
				setMyInterval(i, obj, rowHeight, speed);
			});
		}

		this.each(function(i){
			var sh = opts["rowHeight"],speed = opts["speed"],_this = $(this);
			bak(_this);
			setMyInterval(i, _this, sh, speed);
		});

	}

})(jQuery);